// pages/message/message.js
import { shareOptions } from "../../utils/access";
import request from "../../utils/request";
import { setStorage, removeStorage, getStorage } from "../../utils/utils";
const api = require("../../config/api");
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    noLogin: true,
    selectType:0,
    num: null,
    pageData:{},
    slideButtons:[{type: 'warn',text: '清除',data: 2,}],
    slideButtons2:[{type: 'warn',text: '清除',data: 1,}],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getNoticeNumber()

    // 清除缓存中未查看兑换券数量
    if(getStorage('newMsgNum')){
      // 请求接口去除红点
      this.hideRedPoint()
    }
  },
  // 去除消息红点
  async hideRedPoint(){
    const res = await request.get(api.removeMessage, {}, {
      loading: false,
      modal: false
    })
    if(res.httpStatus === 200){
      removeStorage('newMsgNum')
    }
  },
  getNoticeNumber: async function(){
    if (!app.isLogin()) return
    const res = await request.get(api.noticeCount,{}, {modal: false})
      if(res.httpStatus==200){
        console.log('res',res);
        let systemNumber=res.data.systemNumber || 0
        let waitNumber=res.data.waitNumber || 0
        let num=systemNumber+waitNumber
        this.setData({
          pageData:res.data,
          noLogin: !app.isLogin(),
          num
        })
      } else {
        this.setData({
          pageData: null,
          noLogin: !app.isLogin(),
          num: 0
        })
      }
  },
  //跳转详情
  clickItem:function(e){
    if (!app.isLogin()) {
      setStorage('redirectPath', '/pages/message/message')
      app.login()
      return
    }
    let index = e.currentTarget.dataset.index
    if(index==1){
      wx.navigateTo({
        url: '/pages/sysMessage/sysMessage',
      })
    }else{
      wx.navigateTo({
        url: '/pages/dynaMessage/dynaMessage',
      })
    }
  },

  //左滑清除
  slideButtonTap(e) {
    if (!app.isLogin()) {
      setStorage('redirectPath', '/pages/message/message')
      app.login()
      return
    }
    this.setData({
      selectType:e.detail.data
    })
    this.modal.show()
  },
  // 确认弹窗
	async confirm(e) {
    let {selectType} = this.data
    const res = await request.post(api.clearMessage+selectType,{}, {modal: false})
    if(res.httpStatus==200){
      this.modal.hide()
      this.getNoticeNumber()
      console.log('res',res);
    } else {
      this.modal.hide()
      this.setData({
        pageData: null,
        noLogin: !app.isLogin(),
        num: 0
      })
    }
	},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.modal = this.selectComponent('#modal')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getTabBar?.()?.setData?.({
			selected: 3,
    })
    this.getNoticeNumber()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // 用户点击右上角分享
	onShareAppMessage(e) {
		return shareOptions('default', e)
	},
})